package com.example.ttkl.Base;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.ttkl.Constants;
import com.example.ttkl.fragment.XYXActivity.PT.ActivityManager;
import com.example.ttkl.interfaces.IBasePresenter;
import com.example.ttkl.interfaces.IBaseView;
import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;

import java.util.List;


public abstract class BaseActivity<P extends IBasePresenter> extends AppCompatActivity implements IBaseView {
    public P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.addActivity(this);
        //设计模式中的模板方法调用
        setContentView(getLayout());
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//输入法不会将界面顶起
        initView();
        setPermission();
        mPresenter = initPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
            initData();
        }
        initListener();
    }

    protected abstract P initPresenter();

    protected abstract int getLayout();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initListener();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        ActivityManager.removeActivity(this);
    }

    //动态申请普通危险权限
    private void setPermission() {
        String[] strings = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.INTERNET,
                Manifest.permission.VIBRATE,
        };
        PermissionsUtil.requestPermission(this, new PermissionListener() {
            @Override
            public void permissionGranted(@NonNull String[] permission) {
                Log.i("TAG", "permissionGranted: " + "权限请求成功");
            }

            @Override
            public void permissionDenied(@NonNull String[] permission) {
                Log.i("TAG", "permissionDenied: " + "权限请求失败");
            }
        }, strings);
    }


}
